{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.1 \u8bfb\u5199CSV\u6570\u636e\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u8bfb\u5199\u4e00\u4e2aCSV\u683c\u5f0f\u7684\u6587\u4ef6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u5927\u591a\u6570\u7684CSV\u683c\u5f0f\u7684\u6570\u636e\u8bfb\u5199\u95ee\u9898\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528 csv \u5e93\u3002\n\u4f8b\u5982\uff1a\u5047\u8bbe\u4f60\u5728\u4e00\u4e2a\u540d\u53ebstocks.csv\u6587\u4ef6\u4e2d\u6709\u4e00\u4e9b\u80a1\u7968\u5e02\u573a\u6570\u636e\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "Symbol,Price,Date,Time,Change,Volume\n\"AA\",39.48,\"6/11/2007\",\"9:36am\",-0.18,181800\n\"AIG\",71.38,\"6/11/2007\",\"9:36am\",-0.15,195500\n\"AXP\",62.58,\"6/11/2007\",\"9:36am\",-0.46,935000\n\"BA\",98.31,\"6/11/2007\",\"9:36am\",+0.12,104800\n\"C\",53.08,\"6/11/2007\",\"9:36am\",-0.25,360900\n\"CAT\",78.29,\"6/11/2007\",\"9:36am\",-0.23,225400"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u5411\u4f60\u5c55\u793a\u5982\u4f55\u5c06\u8fd9\u4e9b\u6570\u636e\u8bfb\u53d6\u4e3a\u4e00\u4e2a\u5143\u7ec4\u7684\u5e8f\u5217\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import csv\nwith open('stocks.csv') as f:\n    f_csv = csv.reader(f)\n    headers = next(f_csv)\n    for row in f_csv:\n        # Process row\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c row \u4f1a\u662f\u4e00\u4e2a\u5217\u8868\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u8bbf\u95ee\u67d0\u4e2a\u5b57\u6bb5\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u4e0b\u6807\uff0c\u5982 row[0] \u8bbf\u95eeSymbol\uff0c row[4] \u8bbf\u95eeChange\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7531\u4e8e\u8fd9\u79cd\u4e0b\u6807\u8bbf\u95ee\u901a\u5e38\u4f1a\u5f15\u8d77\u6df7\u6dc6\uff0c\u4f60\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u547d\u540d\u5143\u7ec4\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from collections import namedtuple\nwith open('stock.csv') as f:\n    f_csv = csv.reader(f)\n    headings = next(f_csv)\n    Row = namedtuple('Row', headings)\n    for r in f_csv:\n        row = Row(*r)\n        # Process row\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b83\u5141\u8bb8\u4f60\u4f7f\u7528\u5217\u540d\u5982 row.Symbol \u548c row.Change \u4ee3\u66ff\u4e0b\u6807\u8bbf\u95ee\u3002\n\u9700\u8981\u6ce8\u610f\u7684\u662f\u8fd9\u4e2a\u53ea\u6709\u5728\u5217\u540d\u662f\u5408\u6cd5\u7684Python\u6807\u8bc6\u7b26\u7684\u65f6\u5019\u624d\u751f\u6548\u3002\u5982\u679c\u4e0d\u662f\u7684\u8bdd\uff0c\n\u4f60\u53ef\u80fd\u9700\u8981\u4fee\u6539\u4e0b\u539f\u59cb\u7684\u5217\u540d(\u5982\u5c06\u975e\u6807\u8bc6\u7b26\u5b57\u7b26\u66ff\u6362\u6210\u4e0b\u5212\u7ebf\u4e4b\u7c7b\u7684)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u5916\u4e00\u4e2a\u9009\u62e9\u5c31\u662f\u5c06\u6570\u636e\u8bfb\u53d6\u5230\u4e00\u4e2a\u5b57\u5178\u5e8f\u5217\u4e2d\u53bb\u3002\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import csv\nwith open('stocks.csv') as f:\n    f_csv = csv.DictReader(f)\n    for row in f_csv:\n        # process row\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5217\u540d\u53bb\u8bbf\u95ee\u6bcf\u4e00\u884c\u7684\u6570\u636e\u4e86\u3002\u6bd4\u5982\uff0crow['Symbol'] \u6216\u8005 row['Change']"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u5199\u5165CSV\u6570\u636e\uff0c\u4f60\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528csv\u6a21\u5757\uff0c\u4e0d\u8fc7\u8fd9\u65f6\u5019\u5148\u521b\u5efa\u4e00\u4e2a writer \u5bf9\u8c61\u3002\u4f8b\u5982:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "headers = ['Symbol','Price','Date','Time','Change','Volume']\nrows = [('AA', 39.48, '6/11/2007', '9:36am', -0.18, 181800),\n         ('AIG', 71.38, '6/11/2007', '9:36am', -0.15, 195500),\n         ('AXP', 62.58, '6/11/2007', '9:36am', -0.46, 935000),\n       ]\n\nwith open('stocks.csv','w') as f:\n    f_csv = csv.writer(f)\n    f_csv.writerow(headers)\n    f_csv.writerows(rows)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u5178\u5e8f\u5217\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u50cf\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']\nrows = [{'Symbol':'AA', 'Price':39.48, 'Date':'6/11/2007',\n        'Time':'9:36am', 'Change':-0.18, 'Volume':181800},\n        {'Symbol':'AIG', 'Price': 71.38, 'Date':'6/11/2007',\n        'Time':'9:36am', 'Change':-0.15, 'Volume': 195500},\n        {'Symbol':'AXP', 'Price': 62.58, 'Date':'6/11/2007',\n        'Time':'9:36am', 'Change':-0.46, 'Volume': 935000},\n        ]\n\nwith open('stocks.csv','w') as f:\n    f_csv = csv.DictWriter(f, headers)\n    f_csv.writeheader()\n    f_csv.writerows(rows)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u5e94\u8be5\u603b\u662f\u4f18\u5148\u9009\u62e9csv\u6a21\u5757\u5206\u5272\u6216\u89e3\u6790CSV\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u4f1a\u50cf\u7f16\u5199\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with open('stocks.csv') as f:\nfor line in f:\n    row = line.split(',')\n    # process row\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u7684\u4e00\u4e2a\u7f3a\u70b9\u5c31\u662f\u4f60\u4ecd\u7136\u9700\u8981\u53bb\u5904\u7406\u4e00\u4e9b\u68d8\u624b\u7684\u7ec6\u8282\u95ee\u9898\u3002\n\u6bd4\u5982\uff0c\u5982\u679c\u67d0\u4e9b\u5b57\u6bb5\u503c\u88ab\u5f15\u53f7\u5305\u56f4\uff0c\u4f60\u4e0d\u5f97\u4e0d\u53bb\u9664\u8fd9\u4e9b\u5f15\u53f7\u3002\n\u53e6\u5916\uff0c\u5982\u679c\u4e00\u4e2a\u88ab\u5f15\u53f7\u5305\u56f4\u7684\u5b57\u6bb5\u78b0\u5de7\u542b\u6709\u4e00\u4e2a\u9017\u53f7\uff0c\u90a3\u4e48\u7a0b\u5e8f\u5c31\u4f1a\u56e0\u4e3a\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef\u5927\u5c0f\u7684\u884c\u800c\u51fa\u9519\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0ccsv \u5e93\u53ef\u8bc6\u522bMicrosoft Excel\u6240\u4f7f\u7528\u7684CSV\u7f16\u7801\u89c4\u5219\u3002\n\u8fd9\u6216\u8bb8\u4e5f\u662f\u6700\u5e38\u89c1\u7684\u5f62\u5f0f\uff0c\u5e76\u4e14\u4e5f\u4f1a\u7ed9\u4f60\u5e26\u6765\u6700\u597d\u7684\u517c\u5bb9\u6027\u3002\n\u7136\u800c\uff0c\u5982\u679c\u4f60\u67e5\u770bcsv\u7684\u6587\u6863\uff0c\u5c31\u4f1a\u53d1\u73b0\u6709\u5f88\u591a\u79cd\u65b9\u6cd5\u5c06\u5b83\u5e94\u7528\u5230\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\u4e0a(\u5982\u4fee\u6539\u5206\u5272\u5b57\u7b26\u7b49)\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8bfb\u53d6\u4ee5tab\u5206\u5272\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Example of reading tab-separated values\nwith open('stock.tsv') as f:\n    f_tsv = csv.reader(f, delimiter='\\t')\n    for row in f_tsv:\n        # Process row\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u6b63\u5728\u8bfb\u53d6CSV\u6570\u636e\u5e76\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a\u547d\u540d\u5143\u7ec4\uff0c\u9700\u8981\u6ce8\u610f\u5bf9\u5217\u540d\u8fdb\u884c\u5408\u6cd5\u6027\u8ba4\u8bc1\u3002\n\u4f8b\u5982\uff0c\u4e00\u4e2aCSV\u683c\u5f0f\u6587\u4ef6\u6709\u4e00\u4e2a\u5305\u542b\u975e\u6cd5\u6807\u8bc6\u7b26\u7684\u5217\u5934\u884c\uff0c\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "Street\u00a0Address,Num-Premises,Latitude,Longitude 5412\u00a0N\u00a0CLARK,10,41.980262,-87.668452"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u6700\u7ec8\u4f1a\u5bfc\u81f4\u5728\u521b\u5efa\u4e00\u4e2a\u547d\u540d\u5143\u7ec4\u65f6\u4ea7\u751f\u4e00\u4e2a ValueError \u5f02\u5e38\u800c\u5931\u8d25\u3002\n\u4e3a\u4e86\u89e3\u51b3\u8fd9\u95ee\u9898\uff0c\u4f60\u53ef\u80fd\u4e0d\u5f97\u4e0d\u5148\u53bb\u4fee\u6b63\u5217\u6807\u9898\u3002\n\u4f8b\u5982\uff0c\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5728\u975e\u6cd5\u6807\u8bc6\u7b26\u4e0a\u4f7f\u7528\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import re\nwith open('stock.csv') as f:\n    f_csv = csv.reader(f)\n    headers = [ re.sub('[^a-zA-Z_]', '_', h) for h in next(f_csv) ]\n    Row = namedtuple('Row', headers)\n    for r in f_csv:\n        row = Row(*r)\n        # Process row\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u91cd\u8981\u7684\u4e00\u70b9\u9700\u8981\u5f3a\u8c03\u7684\u662f\uff0ccsv\u4ea7\u751f\u7684\u6570\u636e\u90fd\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\uff0c\u5b83\u4e0d\u4f1a\u505a\u4efb\u4f55\u5176\u4ed6\u7c7b\u578b\u7684\u8f6c\u6362\u3002\n\u5982\u679c\u4f60\u9700\u8981\u505a\u8fd9\u6837\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u4f60\u5fc5\u987b\u81ea\u5df1\u624b\u52a8\u53bb\u5b9e\u73b0\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u5728CSV\u6570\u636e\u4e0a\u6267\u884c\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "col_types = [str, float, str, str, float, int]\nwith open('stocks.csv') as f:\n    f_csv = csv.reader(f)\n    headers = next(f_csv)\n    for row in f_csv:\n        # Apply conversions to the row items\n        row = tuple(convert(value) for convert, value in zip(col_types, row))\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u5916\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u8f6c\u6362\u5b57\u5178\u4e2d\u7279\u5b9a\u5b57\u6bb5\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print('Reading as dicts with type conversion')\nfield_types = [ ('Price', float),\n                ('Change', float),\n                ('Volume', int) ]\n\nwith open('stocks.csv') as f:\n    for row in csv.DictReader(f):\n        row.update((key, conversion(row[key]))\n                for key, conversion in field_types)\n        print(row)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u6765\u8bb2\uff0c\u4f60\u53ef\u80fd\u5e76\u4e0d\u60f3\u8fc7\u591a\u53bb\u8003\u8651\u8fd9\u4e9b\u8f6c\u6362\u95ee\u9898\u3002\n\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0cCSV\u6587\u4ef6\u90fd\u6216\u591a\u6216\u5c11\u6709\u4e9b\u7f3a\u5931\u7684\u6570\u636e\uff0c\u88ab\u7834\u574f\u7684\u6570\u636e\u4ee5\u53ca\u5176\u5b83\u4e00\u4e9b\u8ba9\u8f6c\u6362\u5931\u8d25\u7684\u95ee\u9898\u3002\n\u56e0\u6b64\uff0c\u9664\u975e\u4f60\u7684\u6570\u636e\u786e\u5b9e\u6709\u4fdd\u969c\u662f\u51c6\u786e\u65e0\u8bef\u7684\uff0c\u5426\u5219\u4f60\u5fc5\u987b\u8003\u8651\u8fd9\u4e9b\u95ee\u9898(\u4f60\u53ef\u80fd\u9700\u8981\u589e\u52a0\u5408\u9002\u7684\u9519\u8bef\u5904\u7406\u673a\u5236)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u5982\u679c\u4f60\u8bfb\u53d6CSV\u6570\u636e\u7684\u76ee\u7684\u662f\u505a\u6570\u636e\u5206\u6790\u548c\u7edf\u8ba1\u7684\u8bdd\uff0c\n\u4f60\u53ef\u80fd\u9700\u8981\u770b\u4e00\u770b Pandas \u5305\u3002Pandas \u5305\u542b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u51fd\u6570\u53eb pandas.read_csv() \uff0c\n\u5b83\u53ef\u4ee5\u52a0\u8f7dCSV\u6570\u636e\u5230\u4e00\u4e2a DataFrame \u5bf9\u8c61\u4e2d\u53bb\u3002\n\u7136\u540e\u5229\u7528\u8fd9\u4e2a\u5bf9\u8c61\u4f60\u5c31\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u5f62\u5f0f\u7684\u7edf\u8ba1\u3001\u8fc7\u6ee4\u6570\u636e\u4ee5\u53ca\u6267\u884c\u5176\u4ed6\u9ad8\u7ea7\u64cd\u4f5c\u4e86\u3002\n\u57286.13\u5c0f\u8282\u4e2d\u4f1a\u6709\u8fd9\u6837\u4e00\u4e2a\u4f8b\u5b50\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}